/**
 * 
 */
package controller.xml;

import java.io.File;

import model.preprocessor.PreProcessorFactory;
import model.session.CNFUse;
import model.session.Session;
import model.solver.SolverFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Use to parse a {@link Session} exported in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class SessionHandler extends DefaultHandler {

	private Session				session		= null;
	private File				directory;
	private SolverFactory		solverfact;
	private PreProcessorFactory	preprocfact;
	private SessionXMLStatus	status		= SessionXMLStatus.NONE;
	protected XMLHandler		delegate	= null;


	public SessionHandler(File directory, SolverFactory solverfact, PreProcessorFactory preprocfact) {
		this.preprocfact = preprocfact;
		this.solverfact = solverfact;
		this.directory = directory;

	}


	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		System.out.println("Characters: " + new String(ch, start, length));
		if (delegate != null)
			if (delegate.characters(ch, start, length))
				return;
		throw new SAXException("unnexpecte behavior");
	}

	@Override
	public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		System.out.println("Start element: " + arg2);

		if (delegate != null)
			if (delegate.startElement(arg0, arg1, arg2, arg3))
				return;

		startElement2(arg0, arg1, arg2, arg3);
	}


	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println("End element: " + qName);

		if (delegate != null)
			if (delegate.endElement(uri, qName, qName))
				return;
		endElement2(uri, qName, qName);
	}

	public void endElement2(String uri, String localName, String qName) throws SAXException {
		System.out.println("session end element " + qName);

		switch (status) {
		case NONE:
			throw new SAXException("no element to end");

		case CNFUSE:
			if (qName.equals(XMLBaliseFormat.CNFUSE))
			{// ferme cnfuse
				status = SessionXMLStatus.NONE;
				CNFUse use = ((CNFUseXMLHandler) delegate).getCnfuse();
				if (use == null)
					throw new SAXException("problem CNFUSE");

				System.out.println("ajoute session");
				session = new Session(use);
				delegate = null;
				return;
			}
			throw new SAXException("end of CNFUSE was expected");
		}

		throw new SAXException("an other end of element was expected");
	}


	public void startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		System.out.println("start element session" + arg2);

		if (status != SessionXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.CNFUSE))
		{// balise cnfuse
			if (session != null)
				throw new SAXException("already have a CNFUSE/SESSION");

			// set les donnees pour recupere le solver
			status = SessionXMLStatus.CNFUSE;
			delegate = new CNFUseXMLHandler(null, preprocfact, solverfact, directory);
			return;
		}

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}


	/**
	 * Return the session.
	 * 
	 * @return the session
	 */
	public Session getSession() {
		return session;
	}


	@Override
	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
		System.out.println("White space: " + new String(ch, start, length));
	}


}
